package com.sugar.queryinfo.module.business.controller.admin.data.dataquerydtjd;

import com.sugar.queryinfo.module.business.controller.admin.data.dataquerydtjd.vo.DataQueryDtjdPageReqVO;
import com.sugar.queryinfo.module.business.controller.admin.data.dataquerydtjd.vo.DataQueryDtjdRespVO;
import com.sugar.queryinfo.module.business.controller.admin.data.dataquerydtjd.vo.DataQueryDtjdSaveReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.queryinfo.framework.common.pojo.PageParam;
import com.sugar.queryinfo.framework.common.pojo.PageResult;
import com.sugar.queryinfo.framework.common.pojo.CommonResult;
import com.sugar.queryinfo.framework.common.util.object.BeanUtils;
import static com.sugar.queryinfo.framework.common.pojo.CommonResult.success;

import com.sugar.queryinfo.framework.excel.core.util.ExcelUtils;

import com.sugar.queryinfo.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.queryinfo.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.queryinfo.module.business.dal.dataobject.dataquerydtjd.DataQueryDtjdDO;
import com.sugar.queryinfo.module.business.service.dataquerydtjd.DataQueryDtjdService;

@Tag(name = "管理后台 - 数据查询-TC报告")
@RestController
@RequestMapping("/business/data-query-dtjd")
@Validated
public class DataQueryDtjdController {

    @Resource
    private DataQueryDtjdService dataQueryDtjdService;

    @PostMapping("/create")
    @Operation(summary = "创建数据查询-TC报告")
    @PreAuthorize("@ss.hasPermission('business:data-query-dtjd:create')")
    public CommonResult<Long> createDataQueryDtjd(@Valid @RequestBody DataQueryDtjdSaveReqVO createReqVO) {
        return success(dataQueryDtjdService.createDataQueryDtjd(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据查询-TC报告")
    @PreAuthorize("@ss.hasPermission('business:data-query-dtjd:update')")
    public CommonResult<Boolean> updateDataQueryDtjd(@Valid @RequestBody DataQueryDtjdSaveReqVO updateReqVO) {
        dataQueryDtjdService.updateDataQueryDtjd(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据查询-TC报告")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:data-query-dtjd:delete')")
    public CommonResult<Boolean> deleteDataQueryDtjd(@RequestParam("id") Long id) {
        dataQueryDtjdService.deleteDataQueryDtjd(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除数据查询-TC报告")
                @PreAuthorize("@ss.hasPermission('business:data-query-dtjd:delete')")
    public CommonResult<Boolean> deleteDataQueryDtjdList(@RequestParam("ids") List<Long> ids) {
        dataQueryDtjdService.deleteDataQueryDtjdListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据查询-TC报告")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:data-query-dtjd:query')")
    public CommonResult<DataQueryDtjdRespVO> getDataQueryDtjd(@RequestParam("id") Long id) {
        DataQueryDtjdDO dataQueryDtjd = dataQueryDtjdService.getDataQueryDtjd(id);
        return success(BeanUtils.toBean(dataQueryDtjd, DataQueryDtjdRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据查询-TC报告分页")
    @PreAuthorize("@ss.hasPermission('business:data-query-dtjd:query')")
    public CommonResult<PageResult<DataQueryDtjdRespVO>> getDataQueryDtjdPage(@Valid DataQueryDtjdPageReqVO pageReqVO) {
        PageResult<DataQueryDtjdDO> pageResult = dataQueryDtjdService.getDataQueryDtjdPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DataQueryDtjdRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据查询-TC报告 Excel")
    @PreAuthorize("@ss.hasPermission('business:data-query-dtjd:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDataQueryDtjdExcel(@Valid DataQueryDtjdPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DataQueryDtjdDO> list = dataQueryDtjdService.getDataQueryDtjdPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数据查询-TC报告.xls", "数据", DataQueryDtjdRespVO.class,
                        BeanUtils.toBean(list, DataQueryDtjdRespVO.class));
    }

}